<template>
	<view class="home">
		<!-- <view style="margin-top:150rpx;padding:50rpx;height:30vh">供开发测试用5:q:{{q}},id:{{id}},res:{{res}}
			,appid:{{appid}},url1:{{url1}}
		</view> -->
		<!-- 	<uni-nav-bar :background-color="'rgba(0,0,0,0)'" :color="'#fff'" fixed status-bar :border="false"
			left-icon="left" title="抽奖" @clickLeft="back" /> -->

		<turntable17 @handleSubscribe="handleSubscribe" :is_subscribe="is_subscribe" :music_url="music_url"
			:extraData="extraData" v-if="isTurntable">
		</turntable17>
		<scratchcard @handleSubscribe="handleSubscribe" :is_subscribe="is_subscribe" class="scratchcard"
			:music_url="music_url" :extraData="extraData" v-if="isScratchcard">
		</scratchcard>
		<home-dia class="home-dia" :music_url="music_url" v-if="istest" />
		<bottom-adv />

		<tab></tab>

	</view>
</template>

<script>
	import turntable17 from '../components/turntable17' //大转盘
	import scratchcard from '../components/scratchcard' //刮刮乐

	import HomeDia from '../components/HomeDia.vue' //弹出层
	import tab from '@/components/tab.vue' //底部菜单
	// import MusicCom from '../components/MusicCom.vue' 中奖结果通知 SxWYYWTKnxnfgxJA4hnl80iadiC4nx8DtGRCqM59WlU
	// import FloatImg from '../components/FloatImg.vue'
	// import BottomAdv from '../components/BottomAdv.vue'

	export default {

		data() {
			return {
				is_subscribe: false,
				url1: "url1",
				music_url: "",
				extraData: {},
				query: "",
				isScratchcard: false,
				isTurntable: false,
				template: '',
				dom: null,
				istest: true,
				//""显示弹出层  "true" 不显示弹出层 线上版为"false" 在开发测试中有兼容问题,调试大转盘"false",调试刮刮乐为"true"
				stringquery: "",
				q: "testq",
				id: "testid",
				appid: "",
				url: "",
				domain: "",
				res: "res1",
				// id:null,
				// all_user_num:null,//人数
				// user_num:null,//抽奖次数
				// task:null,//活动信息背景图片
				// bag_img:null,
				// ym:'http://121.43.50.8:2306/',
			};
		},
		components: {
			tab,
			HomeDia,
			// FloatImg,
			// MusicCom,
			turntable17,
			scratchcard
			//BottomAdv
		},

		async onLoad(query) {
			console.log("prizequery", query)
			var qid = "";
			if (query.q) {
				// 获取扫描二维码跳转到小程序的参数
				var q = decodeURIComponent(query.q);
				this.q = q;
				console.log(q); // 输出扫描二维码跳转到小程序的参数
				var startIndex = q.indexOf('?');
				if (startIndex !== -1) {
					var paramsString = q.slice(startIndex + 1);
					var paramsArray = paramsString.split('&');
					for (var i = 0; i < paramsArray.length; i++) {
						var param = paramsArray[i].split('=');
						if (param[0] === "id") {
							qid = param[1];
						}
					}
				}

			}



			//uni.setStorageSync('activeId', id, 'session');


			var id = qid ? qid : uni.getStorageSync('activeId', 'session');
			this.id = id;
			if (query.id) {
				this.id = query.id
			}

			// if (query.q) {
			// 	var stringquery = decodeURIComponent(query.q)
			// 	id = stringquery.split("?")[1].split("=")[1].id;
			// 	this.stringquery = stringquery;
			// } else { extraData
			// 	id = query.id
			// }

			var referrerInfo = uni.getLaunchOptionsSync().referrerInfo; //获取小程序跳转传过来的数据集


			this.query = JSON.stringify(referrerInfo)
			console.log("this.query", this.query)
			if (referrerInfo.extraData) {

				var d = referrerInfo.extraData;
				uni.setStorageSync('extraData', JSON.stringify(d), 'session');
				console.log("d", d)
				this.extraData = d;
				if (d.id) {
					this.id = d.id
				}
				if (d.appid) {
					this.appid = d.appid
				}
				if (d.url) {
					this.url = d.url
				}
				if (d.domain) {
					this.domain = d.domain
				}
			} else {

				var extraData = uni.getStorageSync('extraData', 'session');
				console.log("extraData1", extraData)
				if (extraData) {
					extraData = JSON.parse(extraData)
					console.log("extraData2", extraData)
					if (extraData.id) {
						this.id = extraData.id
					}
					if (extraData.appid) {
						this.appid = extraData.appid
					}
					if (extraData.url) {
						this.url = extraData.url
					}
					if (extraData.domain) {
						this.domain = extraData.domain
					}
				}
			}

			const pages = getCurrentPages()
			const currentPage = pages[pages.length - 1]
			const route = currentPage.route
			//id = 270
			var path = route + "?id=" + this.id
			uni.setStorageSync('currentPage', path, 'session');

			//var res = await uni.$http.get("/wxuser/pc/info?id=217 231" 182 210);
			var url = "/user/public/taskInfo?task_id=" + this.id; //获取活动数据

			//this.isTurntable = true
			//this.isTurntable = true
			var res = await uni.$http.get(url); //获取活动数据

			if (res.data.code == 1) {

				var d = res.data.data;
				this.res = JSON.stringify(d);
				var template_name = d.task.template.template_name;
				console.log("template_name", template_name)


				if (template_name.indexOf("turntable") > -1) { // 活动模板是否是大转盘
					this.isTurntable = true
				}
				if (template_name.indexOf("scratchcard") > -1) { // 活动模板是否是刮刮乐
					this.isScratchcard = true
				}
				uni.setStorageSync('taskInfo', JSON.stringify(d), 'session');
				uni.setStorageSync('template', d.task.template.template_name, 'session');
				console.log('taskInfo', JSON.parse(uni.getStorageSync('taskInfo', 'session')))
			}

			var url2 = "/wxuser/pc/info?id=" + this.id; //获取活动数据
			var res2 = await uni.$http.get(url2);
			if (res2.data.code == 1) {
				var d2 = res2.data.data;
				this.music_url = d2.setting.music_url; //音乐地址
			}
			this.indexInfo(this.id)


			this.rewardedVideoAd = uni.createRewardedVideoAd({
				adUnitId: 'adunit-54c4a06c561e267f' // 替换为你的激励视频广告位ID
			})

			// 监听激励视频广告加载成功事件
			this.rewardedVideoAd.onLoad(() => {
				this.rewardedVideoAd.show()
					.then(() => console.log('激励视频广告显示成功'))
					.catch(err => console.error('激励视频广告显示失败', err))
				console.log('激励视频广告加载成功')
			})

			// 监听激励视频广告关闭事件
			this.rewardedVideoAd.onClose((res) => {
				if (res.isEnded) {
					// 用户观看完整广告
					console.log('用户观看完整广告')
					this.handleTask(); //用户获取激励
					this.rewardedVideoAd.offClose() // 移除关闭事件监听
					this.rewardedVideoAd.destroy()
				} else {
					// 用户未观看完整广告
					this.rewardedVideoAd.offClose() // 移除关闭事件监听
					this.rewardedVideoAd.destroy()
					console.log('用户未观看完整广告')
				}
			})

			// 监听激励视频广告错误事件
			this.rewardedVideoAd.onError((err) => {
				console.log('激励视频广告错误', err)
			})

		},
		created() {
			// let id = JSON.parse(sessionStorage.getItem("taskInfo"));
			// console.log(id);
			// this.getNumfn()
			// this.datainfofn()

		},
		methods: {
			handleSubscribe(val) {
				this.is_subscribe = val
			},
			async indexInfo(id) {
				var url = "/wxuser/index/info?id=" + id;
				var res = await uni.$http.get(url);
				res = res.data;
				if (res.code == 1) {
					this.is_subscribe = res.data.is_subscribe; //是否订阅抽奖
				}


			},
			async handleTask() { //获取抽奖激励
				let task = JSON.parse(uni.getStorageSync('taskInfo', 'session')).task;
				if (this.advert.data) {

					var res = await uni.$http.post("/wxuser/index/advertRecord", {
						id: task.id
					})
					uni.showToast({
						title: "获取奖励成功",
						icon: "success"
					})

				}
			},
			back() {

			}

		},
		beforeDestroy() {

		},
	};
</script>

<style lang="scss">
	.home {
		//display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		height: 100vh;

		&::-webkit-scrollbar {
			/*滚动条整体样式*/
			width: 0;
			/*高宽分别对应横竖滚动条的尺寸*/
			height: 0;
		}

		&::-webkit-scrollbar-thumb {
			/*滚动条里面小方块*/
			border-radius: 10rpx;
			background: #bbb;
		}

		&::-webkit-scrollbar-track {
			/*滚动条里面轨道*/
			border-radius: 10rpx;
			background: #ededed;
		}

		// .scratchcard {
		// 	position: absolute;
		// 	z-index: 1;
		// 	left: 0;
		// 	top: 0;
		// 	right: 0;
		// 	bottom: 1;
		// }
	}
</style>